<script setup lang="ts">
// import { deleteOrderAPI } from '@/apis/consult';
import { useShowPre } from '@/composables/useShowPre';
import { OrderType } from '@/enums'
import router from '@/router'
import type { ConsultOrderItem } from '@/types/consult'
// import { showFailToast, showSuccessToast } from 'vant';
// import { ref } from 'vue'
//引入ConsultMore 组件
import ConsultMore from './ConsultList.vue'
// import { useCancelOrder } from '@/composables'
// const { cancelLoading, onCancelOrder } = useCancelOrder()
const { showPre } = useShowPre()
defineOptions({
    name: 'ConsultItem'
})
// 接收父组件提交过来的数据
const props = defineProps<{
    item: ConsultOrderItem
}>()

// const showPopover = ref(false)
// //通过actions 属性来定义菜单选项
// const actions = computed(() => [
//     { text: '查看处方', disabled: !props.item.prescriptionId },
//     { text: '删除订单' }
// ])

// const onSelect = () => (action: { text: string }, index: number) => {
//     if (index === 0) showPre(props.item.prescriptionId)
//     if (index === 1) onDeleteOrder(props.item)
// }
import { useCancelOrder, useDeleteOrder } from '@/composables'
const { cancelLoading, onCancelOrder } = useCancelOrder()

const { deleteLoading, onDeleteOrder } = useDeleteOrder(() => {
    emit('on-delete', props.item.id)
})

//为defineEmits 进行类型标注
const emit = defineEmits<{
    'on-delete': [id: string]
}>()
//删除订单 loading
// const deleteLoading = ref(false)
// const onDeleteOrder = async (item: ConsultOrderItem) => {
//     try {
//         deleteLoading.value = true
//         await deleteOrderAPI(item.id)
//         showSuccessToast('删除订单成功')
//         emit('on-delete', item.id)
//     } catch (error) {
//         showFailToast('删除订单失败')
//     } finally {
//         deleteLoading.value = false
//     }
// }
</script>

<template>
    <div class="consult-item">
        <div class="head van-hairline--bottom">
            <img class="img" src="@/assets/avatar-doctor.svg" />
            <p>{{ item.docInfo?.name || '暂未分配医生' }}</p>
            <span :class="{
                orange: item.status === OrderType.ConsultPay,
                green: item.status === OrderType.ConsultChat
            }">
                {{ item.statusValue }}</span>
        </div>
        <div class="body" @click="router.push(`/user/consult/${item.id}`)">
            <div class="body-row">
                <div class="body-label">病情描述</div>
                <div class="body-value">{{ item.illnessDesc }}</div>
            </div>
            <div class="body-row">
                <div class="body-label">价格</div>
                <div class="body-value">¥ {{ item.payment?.toFixed(2) || 0 }}</div>
            </div>
            <div class="body-row">
                <div class="body-label">创建时间</div>
                <div class="body-value tip">{{ item.createTime }}</div>
            </div>
        </div>
        <!-- 按钮 -->
        <!-- 待支付的按钮 -->
        <div class="foot" v-if="item.status === OrderType.ConsultPay">
            <van-button class="gray" plain size="small" round :loading="cancelLoading"
                @click="onCancelOrder(item)">取消问诊</van-button>
            <van-button type="primary" plain size="small" round :to="`/user/consult/${item.id}`">去支付</van-button>
        </div>
        <!-- 已完成 -->
        <div class="foot" v-if="item.status === OrderType.ConsultComplete">
            <!-- <div class="more"> -->
            <van-popover v-model:show="showPopover" :actions="actions" @select="onSelect" placement="top-start">
                <template #reference> 更多 </template>
            </van-popover>
            <!-- </div> -->
            <!-- 使用consult-more组件 -->
            <consult-more :disabled="!item.prescriptionId" @on-show="showPre(item.prescriptionId)"
                @on-delete="onDeleteOrder(item)"></consult-more>
            <van-button class="gray" plain size="small" round :to="`/room?orderId=${item.id}`">
                问诊记录
            </van-button>
            <van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`" v-if="item.evaluateId">
                查看评价
            </van-button>
            <van-button v-else type="primary" plain size="small" round :to="`/room?orderId=${item.id}`">
                写评价
            </van-button>
        </div>
        <!-- 咨询中 -->
        <div class="foot" v-if="item.status === OrderType.ConsultChat">
            <van-button class="gray" plain size="small" round v-if="item.prescriptionId">
                查看处方
            </van-button>
            <van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`">
                继续沟通
            </van-button>
        </div>
        <!-- 待接诊 -->
        <div class="foot" v-if="item.status === OrderType.ConsultWait">
            <van-button class="gray" plain size="small" round :loading="cancelLoading" @click="onCancelOrder(item)">
                取消问诊
            </van-button>
            <van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`">
                继续沟通
            </van-button>
        </div>
        <!-- 取消订单 -->
        <div class="foot" v-if="item.status === OrderType.ConsultCancel">
            <van-button class="gray" plain size="small" round :loading="deleteLoading" @click="onDeleteOrder(item)">
                删除订单
            </van-button>
            <van-button type="primary" plain size="small" round to="/">
                咨询其他医生
            </van-button>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.consult-item {
    border-radius: 4px;
    box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
    background-color: #fff;
    margin-bottom: 10px;

    .head {
        display: flex;
        align-items: center;
        height: 50px;
        padding: 0 15px;

        .img {
            width: 20px;
            height: 20px;
        }

        >p {
            flex: 1;
            font-size: 15px;
            padding-left: 10px;
        }

        >span {
            color: var(--cp-tag);

            &.orange {
                color: #f2994a;
            }

            &.green {
                color: var(--cp-primary);
            }
        }
    }

    .body {
        padding: 15px 15px 8px 15px;

        .body-row {
            display: flex;
            margin-bottom: 7px;
        }

        .body-label {
            width: 62px;
            font-size: 13px;
            color: var(--cp-tip);
        }

        .body-value {
            width: 250px;

            &.tip {
                color: var(--cp-tip);
            }
        }
    }

    .foot {
        padding: 0 15px 15px 15px;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        .van-button {
            margin-left: 10px;
            padding: 0 16px;

            &.gray {
                color: var(--cp-text3);
                background-color: var(--cp-bg);
            }
        }
    }
}
</style>